package com.zhuanzhuan.dao.impl;
import com.zhuanzhuan.dao.AdminDao;
import com.zhuanzhuan.dao.BaseDao;
import com.zhuanzhuan.entity.Admin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdminDaoMySQLImpl extends BaseDao implements AdminDao {

    //通过用户名和密码获取管理员对象
    @Override
    public Admin getAdminByNameAndPwd(String name, String pwd) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Admin admin = null;

        conn = getConnection();
        try {
            //构造PreparedStatement对象
            String sql = "SELECT * FROM admin WHERE name=? AND password = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, pwd);

            rs = pstmt.executeQuery();
            //验证用户名和密码
            if(rs.next()){
                //从MySQL读取用户信息，并加载到patient对象中
                admin = new Admin();
                admin.setName(name);
                admin.setId(rs.getInt("id"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭数据库连接
            closeAll(conn, pstmt, rs);
        }
        return admin;
    }
}
